<template>
  <div>
    <el-upload
      class="upload"
      drag
      action="https://jsonplaceholder.typicode.com/posts/"
      accept=".xls,.xlsx,.json,.txt"
      :http-request="readExcel"
      multiple>
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>
  </div>
</template>

<script>
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        outputs: [],
        x:[],
        y:[],
      }
    },
    methods: {
      readExcel (e) {//表格导入
        return new Promise(function (resolve,reject) {
          var that = this;
          const files = e.file;
          console.log(files);
          /*if (files.length <= 0) {//如果没有文件名
            return false;
          }
          if (/\.(json|txt)$/.test(files.name.toLowerCase())) {
            console.log("111")
            that.read(e);
          }
          else if (!/\.(xls|xlsx|json|txt)$/.test(files.name.toLowerCase())) {
            this.$Message.error('上传格式不正确，请上传xls或者xlsx格式');
            return false;
          }*/
          const fileReader = new FileReader();
          fileReader.onload = (ev) => {
            try {
              const data = ev.target.result;
              const workbook = XLSX.read(data, {
                type: 'binary'
              });
              console.log(workbook.SheetNames.length)
              const ws=[];
              ws.push({filename:files.name})
              console.log(ws)
              for (var i=0;i<workbook.SheetNames.length;i++){
                ws[i+1]=XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[i]]);
                ws[i+1].push({sheetname:workbook.SheetNames[i]})
              }
              /*ws[0].push({sheetname:workbook.SheetNames[0]})
              const wsname = workbook.SheetNames[0];//取第一张表
              const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]);//生成json表格内容
              console.log(wsname)*/
              console.log(ws);
              resolve(ws);
            } catch (e) {

              return false;
            }
          };
          fileReader.readAsBinaryString(files);
        })
      },
      read(e){
        console.log("2")
        return new Promise(function (resolve,reject) {
          const files = e.file;
          console.log(files);
          if (files.length <= 0) {//如果没有文件名
            return false;
          } else if (!/\.(xls|xlsx|json)$/.test(files.name.toLowerCase())) {
            //this.$Message.error('上传格式不正确，请上传xls或者xlsx格式');
            console.error('上传格式不正确，请上传xls或者xlsx格式')
            return false;
          }
          const fileReader = new FileReader();
          fileReader.onload = (ev) => {
            try {
              var result={};
              const data = JSON.parse(ev.target.result);
              result.data=data;
              result.name=files.name;
              result.value=true,
                console.log(result)
              resolve(result);
            } catch (e) {
              return false;
            }
          };
          fileReader.readAsText(files,'gb2312');
        })
      },
    }
  }
</script>

<style scoped>

</style>

